<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false"
    :close-on-press-escape="false">
    <el-form v-loading="loading" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()"
      :label-width="$i18n.locale === 'en-US' ? '120px' : '120px'">
      <el-form-item :label="$t('sysRuleSort.name')" prop="name">
        <!--文本框-->
        <el-input v-model="dataForm.name" :placeholder="$t('sysRuleSort.name')" clearable></el-input>
      </el-form-item>
      <el-form-item :label="$t('sysRuleSort.value')" prop="value">
        <!--文本框-->
        <el-input v-model="dataForm.value" :placeholder="$t('sysRuleSort.value')" clearable></el-input>
      </el-form-item>
      <el-form-item :label="$t('sysRuleSort.sort')" prop="sort">
        <!--文本框-->
        <el-input-number v-model="dataForm.sort" :placeholder="$t('sysRuleSort.sort')" clearable />
      </el-form-item>
      <el-form-item :label="$t('sysRuleSort.sqlCust')" prop="sqlCust">
        <!--文本框-->
        <el-input type="textarea" autosize v-model="dataForm.sqlCust" :placeholder="$t('sysRuleSort.sqlCustTip')"
          clearable></el-input>
      </el-form-item>
      <el-form-item :label="$t('sysRuleSort.remark')" prop="remark">
        <!--文本框-->
        <el-input v-model="dataForm.remark" :placeholder="$t('sysRuleSort.remark')" clearable></el-input>
      </el-form-item>
      <el-form-item prop="status" :label="$t('sysRuleSort.status')">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="1">{{ $t('sysRuleSort.status1') }}</el-radio>
          <el-radio :label="0">{{ $t('sysRuleSort.status0') }}</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" :loading="loading" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
import aes from '@/utils/aes.js'

export default {
  data () {
    return {
      visible: false,
      loading: false,
      dataForm: {
        id: '',
        sort: '',
        name: '',
        value: '',
        sqlCust: '',
        remark: '',
        status: 1,
        creator: '',
        createDate: '',
        updater: '',
        updateDate: '',
        version: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        sort: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        name: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        value: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        version: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.loading = true
      this.$http.get(`/sys/sysRuleSort/${this.dataForm.id}`).then(({ data: res }) => {
        this.loading = false
        if (res.code !== 0) {
          return
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => { })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.loading = true
        // 加密sql语句
        let params = {
          ...this.dataForm,
          sqlCust: aes.encrypt(this.dataForm.sqlCust, this.$store.state.passwordEncKey)
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/sys/sysRuleSort/', params).then(({ data: res }) => {
          this.loading = false
          if (res.code !== 0) {
            return
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => { })
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
